//
//  MoreViewController.m
//  Item A House
//
//  Created by dllo on 15/5/22.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MoreViewController.h"
#import "AboutViewController.h"
#import "GUAAlertView.h"
#import "OtherViewController.h"
#import "DXAlertView.h"
@interface MoreViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *table;

@end

@implementation MoreViewController
- (void)dealloc
{
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

#pragma mark -
#pragma mark - 创建table
- (void)createTable
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(5, 0, self.view.frame.size.width - 10, self.view.frame.size.height - 49 - 64) style:UITableViewStylePlain];
    [self.table setBackgroundColor:[UIColor whiteColor]];
    self.table.rowHeight = 50;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
    // 隐藏滑动条
    self.table.showsVerticalScrollIndicator = NO;
    // 开始设置headertable
    
    // 设置tableview的footer
    self.table.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    //静止来回晃动
    self.table.bounces = NO;
    [self.table release];
    
    
}

#pragma mark - 点击cell跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            
            AboutViewController *about = [[AboutViewController alloc] init];
            about.hidesBottomBarWhenPushed = YES;
            CATransition *trans = [CATransition animation];
            [self.navigationController pushViewController:about animated:NO];
            trans.type = @"rippleEffect";
            trans.duration = 0.8f;
            [self.navigationController.view.layer addAnimation:trans forKey:nil];

            [about release];
            
        }
        if (indexPath.row == 1) {
            DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"😊" contentText:@"呼叫：400-803-5055" leftButtonTitle:@"呼叫" rightButtonTitle:@"取消"];
            [alert show];
            
            alert.leftBlock = ^() {
                NSLog(@"left button clicked");
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://4008035055"]];
            };
            alert.rightBlock = ^() {
                NSLog(@"right button clicked");
            };
            alert.dismissBlock = ^() { 
                NSLog(@"Do something interesting after dismiss block"); 
            };
        }
        if (indexPath.row == 2) {
            GUAAlertView *x = [GUAAlertView alertViewWithTitle:@"帮助"
                                                       message:@"1.在‘首页’点击选择您喜欢的模块（新房，二手房，租房），可以查询相关信息\n2.频道页顶部可以通过区域，小区名等搜索房源，方便快捷\n3.您登录后可以选择收藏喜欢的房源以便下次查看\n"
                                                   buttonTitle:@"OK"
                                           buttonTouchedAction:^{
                                               NSLog(@"button touched");
                                           } dismissAction:^{
                                               NSLog(@"dismiss");
                                           }];
            
            [x show];
            
        }
        if (indexPath.row == 3) {
            dispatch_async(
                           dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                           , ^{
                               NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
                               
                               NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                               NSLog(@"files :%ld",(unsigned long)[files count]);
                               for (NSString *p in files) {
                                   NSError *error;
                                   NSString *path = [cachPath stringByAppendingPathComponent:p];
                                   if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                                       [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                                   }
                               }
                               [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];});
            
           
        }
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"😊" contentText:@"可以给我们一个评价吗" leftButtonTitle:@"五星好评" rightButtonTitle:@"残忍的拒绝"];
            [alert show];
            
            alert.leftBlock = ^() {
                NSLog(@"left button clicked");
            };
            alert.rightBlock = ^() {
                NSLog(@"right button clicked");
            };
            alert.dismissBlock = ^() {
                NSLog(@"Do something interesting after dismiss block");
            };

           
        }
        if (indexPath.row == 1) {
            GUAAlertView *v = [GUAAlertView alertViewWithTitle:@"更多精彩"
                                                       message:@"敬请期待"
                                                   buttonTitle:@"OK"
                                           buttonTouchedAction:^{
                                               NSLog(@"button touched");
                                           } dismissAction:^{
                                               NSLog(@"dismiss");
                                           }];
            
            [v show];
        }
        if (indexPath.row == 2) {
            OtherViewController *other = [[OtherViewController alloc] init];
            other.hidesBottomBarWhenPushed = YES;
            CATransition *trans = [CATransition animation];
            [self.navigationController pushViewController:other animated:NO];
            trans.type = @"rippleEffect";
            trans.duration = 0.8f;
            [self.navigationController.view.layer addAnimation:trans forKey:nil];
            [other release];
        }
    }
}
-(void)clearCacheSuccess
{
    NSLog(@"清理成功");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"缓存已清除" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert show];
}
#pragma mark -
#pragma mark - required协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 4;
    }
    if (section == 1) {
        return 3;
    }
    return YES;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        static NSString *cellIndentity = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentity];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentity];
            
        }
        // 点击不变色
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        // 在后面加大于号
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                cell.textLabel.text = @"🌲 关于软件";
            }
            if (indexPath.row == 1) {
                cell.textLabel.text = @"☎️ 客服电话 (400-803-5055)";
            }
           
            if (indexPath.row == 2) {
                cell.textLabel.text = @"👋 使用帮助";
            }
            if (indexPath.row == 3) {
                cell.textLabel.text = @"🚮 清除缓存";
            }
            
        }
        return cell;
        
    }
    else{
        
        static NSString *cellIndentity1 = @"cell1";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentity1];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentity1];
        }
        
        // 点击不变色
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        
        if (indexPath.section == 1) {
            
            if (indexPath.row == 0) {
                cell.textLabel.text = @"😄 给我们打分";
            }
            if (indexPath.row == 1) {
                cell.textLabel.text = @"✨ 其他精品应用";
            }
            if (indexPath.row == 2){
                cell.textLabel.text = @"🏠 房产资讯";
            }
        }
        return cell;
    }
}


#pragma mark - 返回分区数
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return 2;
}
#pragma mark - 返回标题数
#pragma mark - 标题
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"帮助";
    }
    else
    {
        return @"其他";
        
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTable];
    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
